﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataCenter;

namespace TestProject
{
    class CronManager
    {
        string[] ggSymbols = new string[0]; //普通的个股symbol
        string[] indexSymbols = new string[0]; //指数symbol
        LinkedList<string> monitoredSymbols = new LinkedList<string>();//监控股票的symbol

        public DataCenter.Models.StockInfo[] ggStocks { get; private set; }
        public DataCenter.Models.StockInfo[] indexStocks { get; private set; }
        public DataCenter.Models.StockInfo[] monitoredStocks { get; private set; }  //存储获取到的监控股票信息
        public DataCenter.Models.BKRanking[] bkRanking { get; private set; }

        public bool cronType { get; private set; }

        public CronManager()
        {
            ggStocks = new DataCenter.Models.StockInfo[0];
            indexStocks = new DataCenter.Models.StockInfo[0];
            monitoredStocks = new DataCenter.Models.StockInfo[0];
            bkRanking = new DataCenter.Models.BKRanking[0];
        }


        public void UpdateGGSymbol(string[] symbols)
        {
            ClearBKSymbol(); //获取个股信息时关闭获取板块排行
            ggSymbols = symbols;
        }

        public void ClearGGSymbols()
        {
            ggSymbols = new string[0];
        }

        public void UpdateBKSymbol(DataCenter.Enums.BKType bkType)
        {
            ClearGGSymbols();
            bkSymbol = bkType;
        }

        public void ClearBKSymbol()
        {
            bkSymbol = DataCenter.Enums.BKType.non;
        }

        public void UpdateIndexSymbols(string[] symbols)
        {
            indexSymbols = symbols;
        }

        public void ClearIndexSymbols()
        {
            indexSymbols = new string[0];
        }

        public bool AddMonitoredSymbol(string symbol)
        {
            bool a = false;
            foreach(var i in this.monitoredSymbols)
            {
                if (i == symbol)
                {
                    a = true;
                    break;
                }
            }
            if (!a)
            {
                this.monitoredSymbols.AddFirst(symbol);
                return true;
            }
            return false;
        }

        public bool RemoveMonitoredSymbol(string symbol)
        {
            return monitoredSymbols.Remove(symbol);
        }

        public void ClearMonitoredSymbols()
        {
            monitoredSymbols.Clear();
        }

        public void ClearAll()
        {
            ggSymbols = new string[0];
            indexSymbols = new string[0];
            monitoredSymbols.Clear();
        }

        public async void StartCron(bool start)
        {
            //判断是否重复启动或暂停
            if (cronType == start)
                return;

            if (ggSymbols.Length == 0 && indexSymbols.Length == 0 && monitoredSymbols.Count == 0 && (int)bkSymbol == 0)
                return;

            cronType = start;

            while (cronType)
            {
                //保存当前股票symbol各数组的长度，方便从接收的数据中获取对应数据。因为很可能在获取数据的过程中各股票数组已经改变
                int ggL = ggSymbols.Length;
                int indexL = indexSymbols.Length;
                int monitoredL = monitoredSymbols.Count;

                string[] l = new string[ggL + indexL + monitoredL];
                Array.Copy(ggSymbols, 0, l, 0, ggL);
                Array.Copy(indexSymbols, 0, l, ggL, indexL);
                Array.Copy(monitoredSymbols.ToArray(), 0, l, ggL+indexL, monitoredL);

                if (l.Length != 0)
                {
                    var stockRes = await DataCenter.DataCenter.GetSSSJ(l);
                    ggStocks = new DataCenter.Models.StockInfo[ggL];
                    indexStocks = new DataCenter.Models.StockInfo[indexL];
                    monitoredStocks = new DataCenter.Models.StockInfo[monitoredL];

                    Array.Copy(stockRes, 0, ggStocks, 0, ggL);
                    Array.Copy(stockRes, ggL, indexStocks, 0, indexL);
                    Array.Copy(stockRes, ggL+indexL, monitoredStocks, 0, monitoredL);
                }

                if ((int)bkSymbol != 0)
                {
                    bkRanking = await DataCenter.DataCenter.GetBKRanking(bkSymbol);
                }

                Console.WriteLine(ggStocks.Length + "  " + indexStocks.Length + " " + monitoredStocks.Length+"   "+bkRanking.Length);

                await Task.Delay(2000);
            }
        }
    }
}
